/*
 * 项目名称:platform-plus
 * 类名称:NcpPersonInfoController.java
 * 包名称:com.platform.modules.ncp.controller
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-02-11 18:03:21        tumin     初版做成
 *
 * Copyright (c) 2019-2019 微同软件
 */
package com.platform.modules.ncp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.utils.RestResponse;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.ncp.entity.NcpPersonInfoEntity;
import com.platform.modules.ncp.service.NcpPersonInfoService;
import com.platform.modules.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author tumin
 * @date 2020-02-11 18:03:21
 */
@Api
@Controller
@RequestMapping("ncp/personinfo")
public class NcpPersonInfoController extends AbstractController {
    @Autowired
    private NcpPersonInfoService ncpPersonInfoService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @ApiOperation("查询所有")
    @RequestMapping("/queryAll")
    @ResponseBody
    @RequiresPermissions("ncp:personinfo:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<NcpPersonInfoEntity> list = ncpPersonInfoService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @ApiOperation("查询分页")
    @GetMapping("/list")
    @ResponseBody
    @RequiresPermissions("ncp:personinfo:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page page = ncpPersonInfoService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param persId 主键
     * @return RestResponse
     */
    @ApiOperation("详情")
    @RequestMapping("/info/{persId}")
    @ResponseBody
    @RequiresPermissions("ncp:personinfo:info")
    public RestResponse info(@PathVariable("persId") String persId) {
        NcpPersonInfoEntity ncpPersonInfo = ncpPersonInfoService.getById(persId);

        return RestResponse.success().put("personinfo", ncpPersonInfo);
    }

    /**
     * 新增
     *
     * @param ncpPersonInfo ncpPersonInfo
     * @return RestResponse
     */
    @ApiOperation("新增")
    @SysLog("新增")
    @RequestMapping("/save")
    @ResponseBody
    @RequiresPermissions("ncp:personinfo:save")
    public RestResponse save(@RequestBody NcpPersonInfoEntity ncpPersonInfo) {

        ncpPersonInfoService.add(ncpPersonInfo);

        return RestResponse.success();
    }

    /**
     * 修改
     *
     * @param ncpPersonInfo ncpPersonInfo
     * @return RestResponse
     */
    @ApiOperation("修改")
    @SysLog("修改")
    @RequestMapping("/update")
    @ResponseBody
    @RequiresPermissions("ncp:personinfo:update")
    public RestResponse update(@RequestBody NcpPersonInfoEntity ncpPersonInfo) {

        ncpPersonInfoService.update(ncpPersonInfo);

        return RestResponse.success();
    }

    /**
     * 根据主键删除
     *
     * @param persIds persIds
     * @return RestResponse
     */
    @ApiOperation("删除")
    @SysLog("删除")
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions("ncp:personinfo:delete")
    public RestResponse delete(@RequestBody String[] persIds) {
        ncpPersonInfoService.deleteBatch(persIds);

        return RestResponse.success();
    }

    /**
     * @Author TuMin
     * @Description 导出
     * @Date 11:00 2020/2/12
     * @Param [params, url]
     * @return com.platform.common.utils.RestResponse
     */
    @ApiOperation("/导出")
    @RequestMapping("/exportExcel")
    @RequiresPermissions("ncp:personinfo:export")
    public void exportExcel(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
        Workbook workbook = ncpPersonInfoService.exportExcel(params);
        String fileName = "网格员数据汇总表"+new SimpleDateFormat("yyyyMMddHHmm").format(new Date())+".xlsx";
        ExcelUtil.downLoadExcel(fileName,response,workbook);
    }


    /**
     * @Author TuMin
     * @Description 导入保存
     * @Date 21:40 2020/2/12
     * @Param [file]
     * @return com.platform.common.utils.RestResponse
     */
    @RequestMapping("/importExcel")
    @ResponseBody
    @RequiresPermissions("ncp:personinfo:import")
    public RestResponse importExcel(@RequestParam MultipartFile file){
        ncpPersonInfoService.importExcel(file);
        return RestResponse.success();
    }
}
